package com.file.csv.write;

import com.opencsv.CSVWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

public class WriteCSV {

    /*
     * @path 路径
     * @strArr 表头
     * @list 数据
     */
    public static void writeFile(String path, String[] Header, List<String[]> dataList) {
        File csv = new File(path);
        if (!csv.exists()) {
            try {
                boolean csvBool = csv.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            CSVWriter writer = new CSVWriter(
                    new OutputStreamWriter(new FileOutputStream(csv),
                            "GBK"),
                    CSVWriter.DEFAULT_SEPARATOR,
                    CSVWriter.DEFAULT_QUOTE_CHARACTER);
            writer.writeNext(Header);
            writer.writeAll(dataList);
            writer.flush();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
